package com.hivekion.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hivekion.common.entity.PagedResultVo;
import com.hivekion.common.entity.SearchInputVo;
import com.hivekion.common.exception.BusinessException;
import com.hivekion.common.security.SecurityUtils;
import com.hivekion.system.domain.SysUserBind;
import com.hivekion.system.domain.vo.SysUserBindCreateInputVo;
import com.hivekion.system.domain.vo.SysUserBindModelVo;
import com.hivekion.system.domain.vo.SysUserBindUpdateInputVo;
import com.hivekion.system.mapper.SysUserBindMapper;
import com.hivekion.system.service.ISysUserBindService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysUserBindServiceImpl implements ISysUserBindService {

      @Autowired
      SysUserBindMapper mapper;

      @Override
      public SysUserBindModelVo getInfo(String roleId) {
	    SysUserBind role = getRoleModel(roleId);
	    SysUserBindModelVo modelVo = new SysUserBindModelVo();
	    BeanUtils.copyProperties(role, modelVo);
	    return modelVo;
      }

      @Override
      public PagedResultVo<SysUserBindModelVo> getList(SearchInputVo inputVo) {
	    LambdaQueryWrapper<SysUserBind> wrapper = Wrappers.lambdaQuery();
	    wrapper.and(
		    StringUtils.isNotBlank(inputVo.getFilter()),
		    t ->
			    t.like(SysUserBind::getUserId, inputVo.getFilter())
				    .or()
				    .like(SysUserBind::getRemark, inputVo.getFilter()));
	    Page<SysUserBind> rolePage = new Page<>(inputVo.getPageNum(), inputVo.getPageSize());
	    rolePage.addOrder(new OrderItem("sort_code", true));
	    rolePage.addOrder(new OrderItem("create_time", true));
	    IPage<SysUserBind> roles = mapper.selectPage(rolePage, wrapper);
	    List<SysUserBindModelVo> list =
		    roles.getRecords().stream()
			    .map(
				    item -> {
					  SysUserBindModelVo vo = new SysUserBindModelVo();
					  BeanUtils.copyProperties(item, vo);
					  return vo;
				    })
			    .collect(Collectors.toList());
	    Integer total = (int) roles.getTotal();
	    PagedResultVo<SysUserBindModelVo> resultVo =
		    new PagedResultVo<SysUserBindModelVo>(inputVo, total, list);
	    return resultVo;
      }

      @Override
      public boolean add(SysUserBindCreateInputVo inputVo) {
	    if (!checkUserBindNameUnique("", inputVo.getUserId())) {
		  throw new BusinessException(500, "新增角色'" + inputVo.getUserId() + "'失败，角色名称已存在");
	    }
	    SysUserBind role = new SysUserBind();
	    BeanUtils.copyProperties(inputVo, role);
	    role.setCreateUserId(SecurityUtils.getUsername());
	    return mapper.insert(role) > 0;
      }

      @Override
      public boolean edit(SysUserBindUpdateInputVo inputVo) {
	    if (!checkUserBindNameUnique(inputVo.getUserId(), "")) {
		  throw new BusinessException(500, "修改角色'" + inputVo.getUserId() + "'失败，角色名称已存在");
	    }
	    SysUserBind role = mapper.selectById(inputVo.getUserId());
	    BeanUtils.copyProperties(inputVo, role);
	    role.setUpdateUserId(SecurityUtils.getUsername());
	    return mapper.updateById(role) > 0;
      }

      @Override
      public boolean remove(String roleId) {
	    return mapper.deleteById(roleId) > 0;
      }

      private SysUserBind getRoleModel(String roleId) {
	    SysUserBind role = mapper.selectById(roleId);
	    if (role == null) {
		  throw new BusinessException(500, "未找到角色信息！");
	    }
	    return role;
      }

      @Override
      public boolean checkUserBindNameUnique(String roleId, String roleName) {
	    return true;
      }

      @Override
      public SysUserBind queryUserBind(String roleId) {
	    return mapper.selectById(roleId);
      }

      @Override
      public SysUserBind queryUserBindByName(String roleName) {
	    LambdaQueryWrapper<SysUserBind> wrapper = Wrappers.lambdaQuery();
	    LambdaQueryWrapper<SysUserBind> queryCondition =
		    wrapper.eq(SysUserBind::getUserId, roleName);
	    return mapper.selectOne(queryCondition);
      }
}
